package GUILayer;
import ControlLayer.CtrShowOrder;
import ModelLayer.Order;
import ModelLayer.OrderDetail;
import ModelLayer.Customer;
import ModelLayer.Address;
import ModelLayer.Country;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class UIShowOrder extends javax.swing.JFrame {
    private CtrShowOrder ctrShowOrder;

    public UIShowOrder() {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        ctrShowOrder = CtrShowOrder.getInstance();
    }

    public void loadOrderForm(int id) {
        Order order             = ctrShowOrder.getOrderByID(id);
        Customer customer       = ctrShowOrder.getCustomerByID(order.getIDCustomer());
        Address addressDelivery = ctrShowOrder.getAddressByID(order.getIDAddressDelivery());
        Country countryDelivery = ctrShowOrder.getCountryByID(addressDelivery.getIDCountry());
        Address addressInvoice  = ctrShowOrder.getAddressByID(order.getIDAddressInvoice());
        Country countryInvoice  = ctrShowOrder.getCountryByID(addressInvoice.getIDCountry());
        String customerInfo = new String();
        String paymentInfo  = new String();

        this.setTitle("Fakturanummer: " + Integer.toString(order.getInvoiceNumber()));

        customerInfo += "Navn:\r\n" + customer.getFirstname() + " " + customer.getLastname() + "\r\n\r\n";
        customerInfo += "Leveringsadresse:\r\n" + addressDelivery.getAddress1() + " " + addressDelivery.getAddress2() + ", " + addressDelivery.getPostcode() + " " + addressDelivery.getCity() + ", " + countryDelivery.getIsoCode() + "\r\n\r\n";
        customerInfo += "Faktureringsadresse:\r\n" + addressInvoice.getAddress1() + " " + addressInvoice.getAddress2() + ", " + addressInvoice.getPostcode() + " " + addressInvoice.getCity() + ", " + countryInvoice.getIsoCode() + "\r\n\r\n";
        txtaCustomerInfo.setText(customerInfo);

        paymentInfo += "Betalingsform:\r\n" + order.getPayment() + " (" + order.getModule() + ")\r\n\r\n";
        paymentInfo += "Totalpris:\t" + Float.toString((float)(order.getTotalProducts() * 1.25)) + "\r\n";
        paymentInfo += "Heraf moms:\t" + Float.toString((float)(order.getTotalProducts() * 0.25)) + "\r\n";
        paymentInfo += "Total Rabat:\t" + Float.toString(order.getTotalDiscounts()) + "\r\n";
        paymentInfo += "Fragt:\t" + Float.toString(order.getTotalShipping()) + "\r\n";
        paymentInfo += "Betalt:\t" + Float.toString(order.getTotalPaid()) + "\r\n";
        txtaPaymentInfo.setText(paymentInfo);

        loadOrderDetailList(id);
    }

    private void loadOrderDetailList(int id) {
        ((DefaultTableModel)tblOrderDetailList.getModel()).setRowCount(0);
        ArrayList<OrderDetail> orderDetailList = ctrShowOrder.getOrderDetailListByOrderID(id);
        for (OrderDetail orderDetail : orderDetailList) {
            ((DefaultTableModel)tblOrderDetailList.getModel()).addRow(
                new Object [] {
                    orderDetail.getIDOrderDetail(),
                    orderDetail.getProductName(),
                    orderDetail.getProductPrice(),
                    orderDetail.getProductQuantity(),
                    orderDetail.getProductReference()
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlCustomerInfo = new javax.swing.JScrollPane();
        txtaCustomerInfo = new javax.swing.JTextArea();
        lblCustomerInfo = new javax.swing.JLabel();
        scrlPaymentInfo = new javax.swing.JScrollPane();
        txtaPaymentInfo = new javax.swing.JTextArea();
        lblPaymentInfo = new javax.swing.JLabel();
        scrlOrderDetailList = new javax.swing.JScrollPane();
        tblOrderDetailList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtaCustomerInfo.setColumns(17);
        txtaCustomerInfo.setEditable(false);
        txtaCustomerInfo.setRows(5);
        scrlCustomerInfo.setViewportView(txtaCustomerInfo);

        lblCustomerInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCustomerInfo.setText("Kunde information:");

        txtaPaymentInfo.setColumns(17);
        txtaPaymentInfo.setRows(5);
        scrlPaymentInfo.setViewportView(txtaPaymentInfo);

        lblPaymentInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPaymentInfo.setText("Betalingsinformation:");

        tblOrderDetailList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Name", "Price", "Quantity", "Reference"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlOrderDetailList.setViewportView(tblOrderDetailList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrlOrderDetailList, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCustomerInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                            .addComponent(scrlCustomerInfo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(scrlPaymentInfo)
                            .addComponent(lblPaymentInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCustomerInfo)
                    .addComponent(lblPaymentInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrlCustomerInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(scrlPaymentInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrlOrderDetailList, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblCustomerInfo;
    private javax.swing.JLabel lblPaymentInfo;
    private javax.swing.JScrollPane scrlCustomerInfo;
    private javax.swing.JScrollPane scrlOrderDetailList;
    private javax.swing.JScrollPane scrlPaymentInfo;
    private javax.swing.JTable tblOrderDetailList;
    private javax.swing.JTextArea txtaCustomerInfo;
    private javax.swing.JTextArea txtaPaymentInfo;
    // End of variables declaration//GEN-END:variables
}
